<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            资产盘点
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">操作状态 </div>
                    <div class="aui-list-item-input">
                        <label><input class="aui-radio" type="radio" value="OnLine" name="type" checked> 在线</label>
                        <label><input class="aui-radio" type="radio" value="OffLine" name="type"> 离线</label>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">扫码方式 </div>
                    <div class="aui-list-item-input">
                        <label><input class="aui-radio" type="radio" value="RFIDType" name="scan" checked> RFID</label>
                        <label><input class="aui-radio" type="radio" value="QCodeType" name="scan"> 二维码</label>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">盘点单号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描盘点单号" id="billCode" value="" onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWO()"></i>
                </div>
            </li>
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">资产编码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描资产编码" id="rsCode" value="">
                    </div>
                </div>
            </li> -->

        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info" style="margin-left:10%;width:30%;" onclick="doSearch()">查询</div>
        <div class="aui-btn aui-btn-info" style="width:30%;margin-left:10%;" onclick="goPage()">进入盘点</div>
        <!-- <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info" style="width:30%;margin-left:10%;" onclick="sqliteGet()">替换数据库</div>
        <div class="aui-btn aui-btn-info" style="margin-left:10%;width:30%;;" onclick="SQL()">查询本地数据库</div>  -->
        <!-- <div class="aui-btn aui-btn-info aui-btn-block" onclick="upLoadData()">离线上数据传</div> -->
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:50px;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 8px;font-weight: bold;">
                        <td algin='center'>资本化在</td>
                        <td algin='center'>成本中心</td>
                        <td algin='center'>应盘点资产编码</td>
                        <td algin='center'>应盘点资产名称</td>
                        <td algin='center'>实盘点资产编码</td>
                        <td algin='center'>实盘点资产名称</td>
                        <td algin='center'>盘点人</td>
                        <td algin='center'>盘点时间</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var sysid = ""; //盘点单据GID
    var billCode = "";//盘点单号
     
    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        if (el.id == "billCode") {
            $("#billCode").select();
        } else if (el.id == "rsCode") {
            $("#rsCode").select();
        }
        idSelector = el.id;
        $("#" + el.id).select();
    }
    //选择盘点单号
    function goSelectWO() {
        api.openWin({
            name: 'rsInventoryPddh',
            url: 'rsInventoryPddh.html',
            pageParam: {
                name: "rsInventory",
                type: "onLine",
            }
        });
    }

    //盘点单号选择后回调函数
    function setBillCode(sysid, billcode) {
        sysid = sysid;
        billcode = billcode;
        document.getElementById('billCode').value = billcode;

    }
    //盘点单号值改变,查询表格数据
    function doSearch() {
        //工单号
        var billCode = $("#billCode").val();
        if (billCode == "" || billCode == null || billCode == undefined) {
            api.toast({
                msg: '盘点单号不能为空',
                duration: 3000
            });
            return;
        }
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getbillCodedetails";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: {
                billCode: billCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var list = myobj.list;
                    var html = "";

                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;background-color: #F5F5F5;color: black;font-size: 8px;'>";
                        html += "<td name='ext02'>" + list[i].ext02 + "</td>";
                        html += "<td name='ext03'>" + list[i].ext03 + "</td>";
                        html += "<td name='rsCode'>" + list[i].rsCode + "</td>";
                        html += "<td name='rsName'>" + list[i].rsName + "</td>";
                        html += "<td name='ext07'>" + list[i].ext07 + "</td>";
                        html += "<td name='ext08'>" + list[i].ext08 + "</td>";
                        html += "<td name='ConfirmId'>" + list[i].ConfirmId + "</td>";
                        html += "<td name='ConfirmDate'>" + list[i].ConfirmDate + "</td>";
                        html += "</tr>";
                    }

                    $api.html($api.byId("tbodyHtml"), html);

                } else if (myobj.code == '1') {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }
    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            if (idSelector == 'billCode') {

                $("#billCode").val(ret);

            } else if (idSelector == 'rsCode') {
                $("#rsCode").val(ret);
                saveQrcode();
            }
        });
    }
    //二维码扫描监听回车事件
    $("#rsCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var rsCode = $(this).val();
        //扫码方式
        var scan = $("input[name='scan']:checked").val();
        //盘点单号非空校验
        if (scan == "RFIDType") {
            api.toast({
                msg: '当前扫码方式为RFID扫码',
                duration: 3000
            });
            return;
        }
        if (keycode == '13') {

            //发货单
            $("#rsCode").val(rsCode);

            //查询表格数据
            saveQrcode();
        }
    });
    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
    //RFID保存
    function saverrfid(ret) {
        //操作状态
        var type = $("input[name='type']:checked").val();
        //扫码方式
        var scan = $("input[name='scan']:checked").val();
        //盘点单号
        var billCode = $("#billCode").val();
        //资产编码
        //      var rsCode = $("#rsCode").val();
        var rsCode = ret;
        //盘点单号非空校验
        if (billCode == "" || billCode == null || billCode == undefined) {
            api.toast({
                msg: '盘点单号不能为空',
                duration: 3000
            });
            return;
        }

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/saveBillCode";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                billCode: billCode,
                rsCode: rsCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });

                    //清空表单
                    doSearch();
                } else if (myobj.code == '1') {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }
    //二维码扫码保存
    function saveQrcode() {
        //操作状态
        var type = $("input[name='type']:checked").val();
        //扫码方式
        var scan = $("input[name='scan']:checked").val();
        //盘点单号
        var billCode = $("#billCode").val();
        //资产编码
        var rsCode = $("#rsCode").val();
        //盘点单号非空校验
        if (scan == "RFIDType") {
            api.toast({
                msg: '当前扫码方式为RFID扫码',
                duration: 3000
            });
            return;
        }
        if (billCode == "" || billCode == null || billCode == undefined) {
            api.toast({
                msg: '盘点单号不能为空',
                duration: 3000
            });
            return;
        }

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/saveQrcode";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                billCode: billCode,
                rsCode: rsCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });

                    //清空表单
                    doSearch();
                    $("#rsCode").val("");
                } else if (myobj.code == '1') {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }
    //调用RFID扫码api
    function getRfidData() {
        //操作状态
        var type = $("input[name='type']:checked").val();
        //扫码方式
        var scan = $("input[name='scan']:checked").val();

        if (scan == "QCodeType") {
            api.toast({
                msg: '当前扫码方式为二维码扫码',
                duration: 3000
            });
            return;
        }
        var moduleRfid = api.require('moduleRfid');
        if (type == 'OnLine') {
            moduleRfid.startActivityForResult({
                appParam: ""
            }, function (ret, err) {
                if (ret) {
                    saverrfid(ret);
                }
            });
        } else if (type == 'OffLine') {
            moduleRfid.startActivityForResult({
                appParam: ""
            }, function (ret, err) {
                if (ret) {
                    sqliteSave(ret);
                }
            });
        }

    }
    //离线盘点
    function sqliteSave(ret) {
        //      var arr = ret.split(":");
        var arr1 = ret.split("\n");
        var arr = [];
        for (var z = 0; z < arr1.length; z++) {
            arr[z] = arr1[z].split(":")[0];
        }
        //        alert(arr)
        var result = Array.from(new Set(arr))
        // alert(result)
        //盘点单号
        var billCode = $("#billCode").val();
        for (var i = 0; i < result.length; i++) {
            //RFID 16进制转换字符串
            result[i] = hexToString(result[i].substring(0, 14));
        }
        //       alert(result)
        if (billCode == "" || billCode == null || billCode == undefined) {
            api.toast({
                msg: '盘点单号不能为空',
                duration: 3000
            });
            return;
        }
        var db = api.require('db');
        db.openDatabase({
            name: 'test'
        }, function (ret, err) {
            if (ret.status) {
                console.log("1：数据库创建成功");

                //创建数据表
                db.executeSql({
                    name: 'test',
                    sql: 'CREATE TABLE if not exists AssetEncoding( ZcCode varchar(255),billCode varchar(255))'
                }, function (ret, err) {
                    if (ret.status) {

                        console.log("2：数据库表AssetEncoding创建成功");

                        for (var x = 0; x < result.length; x++) {
                            //插入数据
                            db.executeSql({
                                name: 'test',

                                sql: " insert into AssetEncoding(ZcCode,billCode) values('" + result[x] + "','" + billCode + "')"

                            }, function (ret, err) {
                                if (ret.status) {
                                    api.hideProgress();
                                    api.toast({
                                        msg: "数据已存至本地数据库",
                                        duration: 3000
                                    });
                                    console.log("3：数据库表数据插入成功");

                                }
                                //  else {
                                //     alert(JSON.stringify(err));
                                // }
                            });
                        }


                    } else {
                        alert(JSON.stringify(err));
                    }
                });
            } else {
                alert(JSON.stringify(err));
            }
        });
    }
    //查询本地数据库
    function sqliteGet() {
        var db = api.require('db');
        db.openDatabase({
            name: 'test'
        }, function (ret, err) {
            db.selectSql({
                name: 'test',
                sql: "REPLACE into Assetencoding(ZcCode, billCode) VALUES('6600123', 'ZC-20221111-0048')"
            }, function (ret, err) {
                if (ret.status) {
                    alert(JSON.stringify(ret));
                    console.log(JSON.stringify(ret));
                } else {
                    alert(JSON.stringify(err));
                }
            });
            // //创建数据表
            // db.executeSql({
            //         name: 'test',
            //         sql: 'CREATE TABLE if not exists AssetEncoding( ZcCode varchar(255),billCode varchar(255))'
            // },function(){

            // });

        });
    }

    //查询本地数据库
    function SQL() {
        var db = api.require('db');
        db.openDatabase({
            name: 'test'
        }, function (ret, err) {
            db.selectSql({
                name: 'test',
                sql: "SELECT count(ZcCode),billCode FROM AssetEncoding group by billCode"
                //sql: 'DELETE FROM AssetEncoding'
                //sql: "ALTER TABLE AssetEncoding ADD billCode VARCHAR(255) "
                //sql: "insert or replace into Assetencoding(ZcCode, billCode) values('6600123','ZC-20221111-0048')"
                //sql: "drop table if exists AssetEncoding"
            }, function (ret, err) {
                if (ret.status) {
                    alert(JSON.stringify(ret));
                    console.log(JSON.stringify(ret));
                } else {
                    alert(JSON.stringify(err));
                }
            });

        });
    }

    //16进制转换字符串
    function hexToString(str) {
        var val = "";
        var len = str.length / 2;
        for (var i = 0; i < len; i++) {
            val += String.fromCharCode(parseInt(str.substr(i * 2, 2), 16));
        }
        return val;
    }
    //跳转页面
    function goPage() {
        var name = "";
        //操作状态
        var type = $("input[name='type']:checked").val();
        //扫码方式
        var scan = $("input[name='scan']:checked").val();
        //盘点单号
        var billCode = $("#billCode").val();
        if (billCode == "" || billCode == null || billCode == undefined) {
            api.toast({
                msg: '盘点单号不能为空',
                duration: 3000
            });
            return;
        }

        if (scan == "QCodeType") {
            name = "rsInventoryQRcode";
            api.openWin({
                name: name,
                url: name + '.html',
                pageParam: {
                    name: "qcmChkBillCreate",
                    billCode: billCode,
                    type: type,
                }
            });
        } else if (scan == "RFIDType") {
            var moduleRfid = api.require('moduleRfid');
            if (type == "OnLine") {
                moduleRfid.startActivityForResult({
                    appParam: ""
                }, function (ret, err) {
                    if (ret) {
                        saverrfid(ret);
                    }
                });
            } else if (type == "OffLine") {
                moduleRfid.startActivityForResult({
                    appParam: ""
                }, function (ret, err) {
                    if (ret) {
                        sqliteSave(ret);
                    }
                });
            }
        }

    }
    function upLoadData() {
        var name = "";
        //盘点单号
        var billCode = $("#billCode").val();
        if (billCode == "" || billCode == null || billCode == undefined) {
            api.toast({
                msg: '盘点单号不能为空',
                duration: 3000
            });
            return;
        }
        var db = api.require('db');
        db.openDatabase({
            name: 'test'
        }, function (ret, err) {
            db.selectSql({
                name: 'test',
                sql: "SELECT * FROM AssetEncoding where logotype = '1' and billCode = '" + billCode + "'"
                // sql: 'DELETE FROM AssetEncoding'
            }, function (ret, err) {
                if (ret.status) {
                    var un = localStorage.getItem('un');
                    var serverUrl = localStorage.getItem('serverUrl');
                    var url = serverUrl + "/app/upLoadData";
                    var aa = JSON.stringify(ret);
                    var myobj = eval("(" + aa + ")");
                    var data = {
                        values: { ////以表单方式提交参数（JSON对象）
                            ZcCode: myobj.data,
                            billCode: billCode,
                            un: un
                        }
                    };
                    api.ajax({
                        url: url,
                        method: 'post',
                        dataType: 'json',
                        timeout: 300,
                        returnAll: false,
                        data: data
                    }, function (ret, err) {
                        if (ret) {
                            var data = JSON.stringify(ret);
                            var myobj = eval("(" + data + ")");
                            //成功
                            if (myobj.code == '0') {

                                api.hideProgress();
                                api.toast({
                                    msg: "保存成功",
                                    duration: 3000
                                });

                                //清空表单
                                $("#rsCode").val("");
                            } else if (myobj.code == '1') {
                                api.hideProgress();
                                api.toast({
                                    msg: myobj.message,
                                    duration: 3000
                                });
                            }
                        } else {
                            api.hideProgress();
                            alert(JSON.stringify(err));
                        }
                    });
                } else {
                    api.hideProgress();
                    alert(JSON.stringify(err));
                }
            });

        });

    }
</script>

</html>